import time

from common.basepage import Basepage
from selenium.webdriver.common.by import By
from common.yaml_handler import result

Host = result["Host"]


class Postpage(Basepage):
    url = Host + '/#/project/job_title'
    """管理元素定位"""
    # 新增岗位按钮
    add_button = (By.XPATH, "//button[@class = 'el-button btn job-add el-button--default']")
    # 下拉所属部门按钮
    drop_down_post = (By.XPATH, "//i[@class = 'el-input__icon el-icon-arrow-down']")
    # 选择部门
    click_post = (By.XPATH, "//div[@class='el-tree-node__content']//span[@class = 'el-tree-node__label']")
    # 岗位名称
    post_name = (By.XPATH, "//input[@placeholder = '请输入岗位名称，2~10字以内']")
    # 排序
    sort = (By.XPATH, "//div[@class = 'el-form-item']//input[@class = 'el-input__inner']")
    # 新增弹窗确定按钮
    determine_button = (By.XPATH, "//button[@class = 'el-button el-button--primary']")
    # 新增成功确认弹窗
    successfully_added = (By.XPATH, "//p[@class = 'el-message__content']")
    # "请选择所属部门"提示
    please_select_department = (By.XPATH, "//div[@class = 'el-form-item__error'][1]")
    # "请输入岗位名称，2~10字以内"提示
    enter_Position = (By.XPATH, "//div[contains(text(), '请输入岗位名称，2~10字以内')]")
    # 修改按钮
    edit_button = (By.CLASS_NAME, "el-icon-edit")
    # 左侧自动化测试按钮
    automated_testing = (By.XPATH, "//div[@class='el-tree-node__children']//div//div//div//div")
    # 删除按钮
    delete_button = (By.CLASS_NAME, "el-icon-delete")
    # 删除确认弹窗，确认按钮
    delete_confirmation = (By.CLASS_NAME, "ok-btn")
    # 删除成功提示弹窗
    delete_succeeded = (By.CLASS_NAME, "el-message__content")
    # 左侧web自动化测试按钮
    web_automated_testing = (By.XPATH, "//div[contains(text(),'web自动化测试')]")
    # 不允许删除弹窗
    delete_failed = (By.XPATH, "//p[contains(text(),'岗位已绑定用户，暂不可删除！')]")

    def add_success_post(self, name, numb):
        """新增成功步骤"""
        self.click(self.add_button)
        time.sleep(1)
        # 下拉所属部门
        self.click(self.drop_down_post)
        time.sleep(1)
        # 选择部门
        self.click(self.click_post)
        time.sleep(1)
        self.fill(self.post_name, name)
        self.fill(self.sort, numb)
        self.click(self.determine_button)
        time.sleep(1)
        el = self.driver.find_element(*self.successfully_added)
        text = el.text
        return text

    def add_without_name_and_post(self):
        """新增不输入部门及名称步骤"""
        self.click(self.add_button)
        time.sleep(1)
        self.click(self.determine_button)
        el_department = self.driver.find_element(*self.please_select_department)
        text_department = el_department.text
        time.sleep(1)
        el_post = self.driver.find_element(*self.enter_Position)
        text_post = el_post.text
        return text_department, text_post

    def add_name_1_character(self, name, numb):
        """岗位名称输入1个字步骤"""
        self.click(self.add_button)
        time.sleep(1)
        # 下拉所属部门
        self.click(self.drop_down_post)
        time.sleep(1)
        # 选择部门
        self.click(self.click_post)
        time.sleep(1)
        self.fill(self.post_name, name)
        self.fill(self.sort, numb)
        self.click(self.determine_button)
        time.sleep(1)
        el = self.driver.find_element(*self.enter_Position)
        text = el.text
        return text

    def edit_succes(self, name):
        """修改步骤"""
        self.click(self.automated_testing)
        self.click(self.edit_button)
        self.clear(self.post_name)
        self.fill(self.post_name, name)
        time.sleep(1)
        self.click(self.determine_button)

    def delete_sucsses(self):
        """删除成功"""
        self.click(self.automated_testing)
        self.click(self.delete_button)
        self.click(self.delete_confirmation)

    def delete_fail(self):
        """删除岗位，岗位下绑定人员"""
        self.click(self.web_automated_testing)
        self.click(self.delete_button)
        self.click(self.delete_confirmation)
        el = self.driver.find_element(*self.delete_failed)
        text = el.text
        return text

    def clear_data(self):
        """清楚数据"""
        self.click()











